//
//  IMChatMoreView.m
//  MLIM
//
//  Created by molon on 15/6/29.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "IMChatMoreView.h"
#import <MLKit.h>
#import "IMCommon.h"

@interface IMChatMoreView()

@property (nonatomic, strong) MLElementButtonBroadcastView *bcView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation IMChatMoreView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.bcView];
        [self addSubview:self.pageControl];
        
    }
    return self;
}

DEALLOC_DLOG

- (CGFloat)preferredHeight
{
    return 113.0f;
}

#pragma mark - getter
- (MLElementButtonBroadcastView *)bcView
{
    if (!_bcView) {
        _bcView = [MLElementButtonBroadcastView new];
        
        WEAKSELF
        [_bcView setDidScrollToPageIndexBlock:^(NSInteger combinationIndex,NSInteger combinationCellCount, NSInteger combinationPageIndex) {
            weakSelf.pageControl.numberOfPages = combinationCellCount;
            weakSelf.pageControl.currentPage = combinationPageIndex;
        }];
    }
    return _bcView;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [UIPageControl new];
        _pageControl.hidesForSinglePage = YES;
        _pageControl.exclusiveTouch = YES;
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
        _pageControl.enabled = NO;
    }
    return _pageControl;
}

#pragma mark - setter
- (void)setCombinations:(NSArray *)combinations
{
    _combinations = combinations;
    
    self.bcView.combinations = combinations;
    
    [self setNeedsLayout];
}

#pragma mark - layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
#define kPageControlHeight 10.0f
#define kPageControlBottomMargin 10.0f
    if (self.pageControl.numberOfPages<=1) {
        self.bcView.frame = CGRectMake(0, kPageControlBottomMargin, self.frameWidth, self.frameHeight-kPageControlBottomMargin*2);
        self.pageControl.frame = CGRectZero;
    }else{
        self.bcView.frame = CGRectMake(0, 0, self.frameWidth, self.frameHeight-kPageControlHeight-kPageControlBottomMargin);
        self.pageControl.frame = CGRectMake(0, self.bcView.frameBottom, self.frameWidth, kPageControlHeight);
    }
    
}

@end
